package com.kclm.ssm.controller;

import com.kclm.ssm.pojo.Student;
import com.kclm.ssm.response.Result;
import com.kclm.ssm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : lvhx
 * @Date: 2025/11/11 14:30
 * @Description:
 */
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 查询所有学生信息
     * @return
     */
    @GetMapping
//    @ResponseBody
    public Result findAll(){
        System.out.println("控制方法findAll");
        try{
            List<Student> students = studentService.findAll();
            return  Result.success("查询所用学生信息成功",students);
        }catch (Exception e){
            e.printStackTrace();
            return  Result.failure("查询学生信息失败");
        }
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public  Result findById(@PathVariable Integer id){
        try{
            Student student = studentService.findById(id);
            if(student!=null){
                return  Result.success("成功查询到id为"+id+"学生",student);
            }else {
                return  Result.failure("id为"+id+"的学生不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  Result.failure("查询id为"+id+"失败");
        }

    }

    /**
     * 保存用户信息
     */
    @PostMapping
    public  Result   save(@RequestBody  Student student){
        try{
            boolean flag =   studentService.save(student);
            return  Result.success("保存用学生信息成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return  Result.failure("保存学员失败！");
        }
    }
    /**
     * 更新用户信息
     */
    @PutMapping
    public  Result   modify(@RequestBody  Student student){
//        logger.info("要更新的用户信息===>{}",student);
        try{
            boolean flag =   studentService.modify(student);
            return  Result.success("更新成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return  Result.failure("更新失败！");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable  Integer id){
        try{
            boolean flag = studentService.delete(id);
            return  Result.success("成功删除id为"+id+"的学生",null);
        }catch (Exception e){
            e.printStackTrace();
            return  Result.failure("删除失败！");
        }
//        return  false;
    }
}
